
#ifndef _PACFILE_H__
#define _PACFILE_H__

#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <poll.h>
#include <dirent.h>
#include <cutils/log.h>
#include <wchar.h>
#include "global.h"


#define _MAX_BLOCK_NUM   5
#define _MAX_NV_BACKUP_FALG_NUM 5
#define PAC_MAGIC       (0xFFFAFFFA)


#ifndef INVALID_HANDLE_VALUE
#define INVALID_HANDLE_VALUE (0)
#endif

//#define _UNICODE
/************************************************************************/
/* File address infomation                                              */
/************************************************************************/
typedef struct _X_BLOCK_T
{	
#ifdef  __cplusplus
	_X_BLOCK_T()
    {
        memset(this,0,sizeof(_X_BLOCK_T));
    }
#endif

	DWORD dwBase;
	DWORD dwSize;
}X_BLOCK_T;

/************************************************************************/
/* File infomation                                                      */
/************************************************************************/
typedef struct _X_FILE_INFO_T
{
#ifdef  __cplusplus
	_X_FILE_INFO_T()
    {
        memset(this,0,sizeof(_X_FILE_INFO_T));
    }
#endif
	
	 char		szID[MAX_PATH];
	 char		szType[MAX_PATH];
	DWORD		dwFlag;
	X_BLOCK_T	arrBlock[_MAX_BLOCK_NUM];
	DWORD		dwBlockCount;
	DWORD       dwCheckFlag;  	
}X_FILE_INFO_T;

typedef struct _X_NV_BACKUP_FLAG_T
{
	 char szFlagName[MAX_PATH];
	DWORD  dwCheck;
}X_NV_BACKUP_FLAG_T;

typedef struct _X_NV_BACKUP_ITEM_T
{
#ifdef  __cplusplus
	_X_NV_BACKUP_ITEM_T()
    {
        memset(this,0,sizeof(_X_NV_BACKUP_ITEM_T));
    }
#endif

	 char szItemName[MAX_PATH];
	WORD   wIsBackup;
	WORD   wIsUseFlag;	
	DWORD  dwID;	
	DWORD  dwFlagCount;
	X_NV_BACKUP_FLAG_T nbftArray[_MAX_NV_BACKUP_FALG_NUM];
}X_NV_BACKUP_ITEM_T;
/************************************************************************/
/* FILE_T struct storing file information                               */
/************************************************************************/

typedef struct _FILE_T
{
#ifdef  __cplusplus
	_FILE_T()
	{
		memset(this,0,sizeof(_FILE_T));
		dwSize = sizeof(_FILE_T);
	}
#endif
	
    DWORD  dwSize;				// size of this struct itself
	 char szFileID[256*2];		// file ID,such as FDL,Fdl2,NV and etc.
	 char szFileName[256*2];     // file name,in the packet bin file,it only stores file name
	                            // but after unpacketing, it stores the full path of bin file
	 char szFileVersion[256*2];  // Reserved now
	int    nFileSize;           // file size
	int    nFileFlag;           // if "0", means that it need not a file, and 
	                            // it is only an operation or a list of operations, such as file ID is "FLASH"
	                            // if "1", means that it need a file
	DWORD  nCheckFlag;          // if "1", this file must be downloaded; 
	                            // if "0", this file can not be downloaded;										
	DWORD  dwDataOffset;        // the offset from the packet file header to this file data
	DWORD  dwCanOmitFlag;		// if "1", this file can not be downloaded and not check it as "All files" 
								//   in download and spupgrade tool.
	DWORD  dwAddrNum;
	DWORD  dwAddr[5];
	DWORD  dwReserved[249];     // Reserved for future,not used now
}FILE_T/*, *PFILE_T*/;

/************************************************************************/
/* BIN_PACKET_HEADER_T struct storing packet header information         */
/************************************************************************/
typedef struct _BIN_PACKET_HEADER_T
{
#ifdef  __cplusplus
	_BIN_PACKET_HEADER_T()
	{
		memset(this,0,sizeof(_BIN_PACKET_HEADER_T));
		dwMagic = PAC_MAGIC;
	}
#endif

	 char szVersion[24*2];		// packet struct version
	DWORD  dwSize;              // the whole packet size;
	 char szPrdName[256*2];		// product name
	 char szPrdVersion[256*2];   // product version
	int    nFileCount;          // the number of files that will be downloaded, the file may be an operation
	DWORD  dwFileOffset;		// the offset from the packet file header to the array of FILE_T struct buffer
	DWORD  dwMode;
	DWORD  dwFlashType;
	DWORD  dwNandStrategy;
	DWORD  dwIsNvBackup;
	DWORD  dwNandPageType;
	 char szPrdAlias[100*2];    // product alias
	DWORD  dwOmaDmProductFlag;
	DWORD  dwIsOmaDM;
	DWORD  dwIsPreload;
    DWORD  dwReserved[200];
	DWORD  dwMagic;
	WORD   wCRC1;
	WORD   wCRC2;
}BIN_PACKET_HEADER_T,*PBIN_PACKET_HEADER_T;


/** Open bin packet file for read or write
  *
  * @param lpszFileName: packet file name
  * @param bOpenFlag: if true, open for read, otherwise open for write
  * @return: true,if open successfully;false,otherwise
  */
   int OpenPac(char* lpszFileName);


/** Unpacket the packet to bin files into one specified directory 
  *
  * @param lpszReleaseDirPath: the directory to release bin files
  *                            If it equals NULL, program will create the directory to system temp directory.
  *                            Suggest to set it null.
  * @param bph: store the packet header
  * @param ppFileArray: store the bin files information into FILE_T struct
  *                     it must be release by the "ReleaseMem" function
  * @return: true,if unpacket successfully;false,otherwise
  */
	//BOOL Unpacket(LPCTSTR lpszReleaseDirPath,BIN_PACKET_HEADER_T &bph,FILE_T ** ppFileArray);
	int Unpacket_m7281(char* lpszReleaseDirPath,BIN_PACKET_HEADER_T* bph,FILE_T ** ppFileArray);

	int Unpacket_m1190(char* lpszReleaseDirPath,BIN_PACKET_HEADER_T* bph,FILE_T ** ppFileArray);
	
	int Unpacket_m0282(char* lpszReleaseDirPath,BIN_PACKET_HEADER_T* bph,FILE_T ** ppFileArray);
/** Close the opened file
  * You can use it or not, for it will be invoked in the destruct function
  *
  */
	void ClosePac();


/** Get released directory
  *
  * @return: the path of released path
  */
	//BYTE* GetReleaseDir();

/** Remove released directory
  *
  * @return: true,if remove successful; false,otherwise
  */
	//BOOL RemoveReleaseDir();

/** Get file item count
  *
  * @return: the number of files in the product
  */
	//int   GetFileCount();

/** Get file item information
  *
  * @param tFileInfo: to store the file information
  * @param index: the index of file in files 
  * @return: the number of files in the product
  */
	//BOOL  GetFileInfo(_X_FILE_INFO_T &tFileInfo,int index);
	//BOOL  GetFileInfo(X_FILE_INFO_T* tFileInfo,int index);

extern struct DownloadCallback mDownloadCallback;


#endif // _PACFILE_H__
